package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.CmsContent;
import com.macro.mall.service.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author super
 * @title: CmsContentController
 * @description: TODO
 * @date 2020/8/31 14:29
 */
@Slf4j
@RestController
@RequestMapping("content")
@Api(tags = "content", description = "内容管理(content)")
public class CmsContentController {


    @Autowired
    private CmsContentService cmsContentService;

    @ApiOperation("查询内容列表")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(value = "title", required = false) String title,
                             @RequestParam(value = "source", required = false) String source,
                             @ApiParam(value = "1-app 2-小程序  3-公众号")@RequestParam(value = "channel", required = false) String channel,
                             @ApiParam(value = "01-新闻 02-学习新闻 03-通知栏 04-政策法规 05-社区通知 06-社区新闻 07-办事指南 08-其他（不在APP中展示） 10-弹窗") @RequestParam(value = "classified", required = false) String classified,
                             @ApiParam(value = "1-首页 2-社区 3-学院 4-学习 5-服务") @RequestParam(value = "popId", required = false) String popId,
                             @ApiParam(value = "审核状态 1-发布 2-审核中 3-审核不通过 4-审核通过 5-无效") @RequestParam(value = "status", required = false) String status,
                             @ApiParam(value = "通过发布时间倒叙: create_time desc 通过数据排序字段正序 sort asc ") @RequestParam(value = "orderBy", required = false) String orderBy,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<CmsContent> cmsContents = cmsContentService.find(title, source, channel, status,orderBy, classified, popId, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(cmsContents));
    }


    @ApiOperation("添加内容")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Valid  @RequestBody CmsContent request) {
        try {
            cmsContentService.saveNotNull(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("查询内容详情")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CmsContent> getById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            CmsContent one = cmsContentService.findOne(id);
            return CommonResult.success(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();
    }


    @ApiOperation("删除内容详情")
    @RequestMapping(value = "/delById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteById(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            cmsContentService.updateStatus(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("删除内容详情(非失效)")
    @RequestMapping(value = "/delByIdTrue", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteByIdTrue(@ApiParam(value = "id") @RequestParam(value = "id", required = true) Integer id) {
        try {
            cmsContentService.deleteById(id);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

    @ApiOperation("更新内容")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Valid @ApiParam(value = "请求报文") @RequestBody CmsContent request) {
        try {
            cmsContentService.updateNotNullById(request);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }



}
